extends Window
class_name CharacterStatsWindow

## 角色属性窗口
## 显示角色的基础属性和装备加成属性

@onready var stats_label: RichTextLabel = $VBoxContainer/ScrollContainer/StatsLabel
@onready var close_button: Button = $VBoxContainer/ButtonContainer/CloseButton
@onready var refresh_button: Button = $VBoxContainer/ButtonContainer/RefreshButton

# PlayerHUD引用，用于获取正确的界面尺寸
var player_hud: Control = null

func _ready():
	_setup_window()
	_connect_signals()
	_update_stats_display()

func _setup_window():
	"""设置窗口属性"""
	title = "角色属性"
	size = Vector2(400, 600)
	min_size = Vector2(300, 400)
	unresizable = false
	always_on_top = true
	close_requested.connect(_on_close_requested)

func _connect_signals():
	"""连接信号"""
	# 连接按钮信号
	if close_button != null:
		close_button.pressed.connect(_on_close_requested)
	if refresh_button != null:
		refresh_button.pressed.connect(_update_stats_display)
	
	# 连接ItemManager信号以自动更新属性
	await get_tree().process_frame
	if ItemManager != null:
		ItemManager.connect("equipment_changed", _on_equipment_changed)
		print("角色属性窗口：信号连接成功")
	else:
		print("角色属性窗口：ItemManager不可用")

func _update_stats_display():
	"""更新属性显示"""
	if ItemManager == null:
		stats_label.text = "[color=red]ItemManager不可用[/color]"
		return
	
	var total_stats = ItemManager.get_total_equipment_stats()
	var base_stats = _get_base_player_stats()
	
	var stats_text = "[center][b][color=gold]角色属性[/color][/b][/center]\n\n"
	
	# 核心战斗属性
	stats_text += "[b][color=red]核心战斗属性[/color][/b]\n"
	if PlayerStatsManager != null:
		var total_attack = PlayerStatsManager.get_total_attack()
		var base_attack = PlayerStatsManager.player_stats.base_attack
		var attack_bonus = PlayerStatsManager.equipment_bonus.attack
		stats_text += "[color=red]攻击力:[/color] %d (%d+%d)\n" % [total_attack, base_attack, attack_bonus]
		
		var total_defense = PlayerStatsManager.get_total_defense()
		var base_defense = PlayerStatsManager.player_stats.defense
		var defense_bonus = PlayerStatsManager.equipment_bonus.defense
		stats_text += "[color=blue]防御力:[/color] %d (%d+%d)\n" % [total_defense, base_defense, defense_bonus]
		
		var current_hp = PlayerStatsManager.get_current_hp()
		var max_hp = PlayerStatsManager.get_max_hp()
		# 计算基础最大生命值（不包括装备加成）
		var level = PlayerStatsManager.get_level()
		var base_max_hp = 120 + (level - 1) * 20  # 基础值 + 等级成长
		var hp_bonus = PlayerStatsManager.equipment_bonus.hp
		stats_text += "[color=green]生命值:[/color] %d/%d (%d+%d)\n" % [current_hp, max_hp, base_max_hp, hp_bonus]
	else:
		stats_text += "[color=red]PlayerStatsManager不可用[/color]\n"
	
	# 基础属性
	stats_text += "\n[b][color=cyan]基础属性[/color][/b]\n"
	stats_text += "[color=red]力量:[/color] %d (+%d)\n" % [base_stats.strength, total_stats.strength]
	stats_text += "[color=green]敏捷:[/color] %d (+%d)\n" % [base_stats.agility, total_stats.agility]
	stats_text += "[color=blue]智力:[/color] %d (+%d)\n" % [base_stats.intelligence, total_stats.intelligence]
	stats_text += "[color=orange]耐力:[/color] %d (+%d)\n" % [base_stats.stamina, total_stats.stamina]
	stats_text += "[color=purple]精神:[/color] %d (+%d)\n" % [base_stats.spirit, total_stats.spirit]
	
	# 总属性
	stats_text += "\n[b][color=yellow]总属性[/color][/b]\n"
	stats_text += "[color=red]总力量:[/color] %d\n" % (base_stats.strength + total_stats.strength)
	stats_text += "[color=green]总敏捷:[/color] %d\n" % (base_stats.agility + total_stats.agility)
	stats_text += "[color=blue]总智力:[/color] %d\n" % (base_stats.intelligence + total_stats.intelligence)
	stats_text += "[color=orange]总耐力:[/color] %d\n" % (base_stats.stamina + total_stats.stamina)
	stats_text += "[color=purple]总精神:[/color] %d\n" % (base_stats.spirit + total_stats.spirit)
	
	# 战斗属性
	stats_text += "\n[b][color=lime]战斗属性[/color][/b]\n"
	stats_text += "暴击率: %.1f%%\n" % (total_stats.critical_rate * 100)
	stats_text += "暴击伤害: %.1f%%\n" % (total_stats.critical_damage * 100)
	stats_text += "冷却缩减: %.1f%%\n" % (total_stats.cooldown_reduction * 100)
	stats_text += "穿透率: %.1f%%\n" % (total_stats.penetration_rate * 100)
	stats_text += "物理防御: %d\n" % total_stats.physical_defense
	stats_text += "闪避率: %.1f%%\n" % (total_stats.dodge_rate * 100)
	stats_text += "格挡率: %.1f%%\n" % (total_stats.block_rate * 100)
	stats_text += "吸血: %.1f%%\n" % (total_stats.life_steal * 100)
	stats_text += "韧性: %.1f%%\n" % (total_stats.toughness * 100)
	stats_text += "移动速度: %.1f%%\n" % (total_stats.movement_speed * 100)
	
	# 特殊属性
	stats_text += "\n[b][color=magenta]特殊属性[/color][/b]\n"
	stats_text += "异常状态抗性: %.1f%%\n" % (total_stats.status_resistance * 100)
	stats_text += "异常状态命中率: %.1f%%\n" % (total_stats.status_hit_rate * 100)
	stats_text += "元素伤害加成: %.1f%%\n" % (total_stats.elemental_damage * 100)
	
	# 生命值和魔法值信息
	if PlayerStatsManager != null:
		stats_text += "\n[b][color=red]生命值信息[/color][/b]\n"
		stats_text += "当前生命值: %d/%d\n" % [PlayerStatsManager.get_current_hp(), PlayerStatsManager.get_max_hp()]
		stats_text += "当前等级: %d\n" % PlayerStatsManager.get_level()
		stats_text += "当前经验: %d/%d\n" % [PlayerStatsManager.get_current_exp(), PlayerStatsManager.get_exp_to_next()]
	
	stats_label.text = stats_text

func _get_base_player_stats() -> Dictionary:
	"""获取玩家基础属性（不包括装备加成）"""
	# 这里可以根据玩家等级、职业等计算基础属性
	# 目前使用固定值，后续可以扩展
	var level = 1
	if PlayerStatsManager != null:
		level = PlayerStatsManager.get_level()
	
	return {
		"strength": 10 + level * 2,
		"agility": 10 + level * 2,
		"intelligence": 10 + level * 2,
		"stamina": 10 + level * 2,
		"spirit": 10 + level * 2
	}

func _on_equipment_changed(_slot, _equipment):
	"""装备变化时更新属性显示"""
	_update_stats_display()

func _on_close_requested():
	"""关闭窗口"""
	hide()

func toggle_visibility():
	"""切换窗口显示状态"""
	if visible:
		hide()
	else:
		show()
		_update_stats_display()

func show_window():
	"""显示窗口"""
	show()
	_update_stats_display()

func hide_window():
	"""隐藏窗口"""
	hide()
